<?php
class Yamarket_YmlController extends Zend_Controller_Action {
    private $mapperCat;
    private $mapperOffers;
    private $mapperSettings;
    public function init() {
        parent::init();
		$this->mapperCat = new Yamarket_Model_CategoriesMapper( );
        $this->mapperOffers = new Yamarket_Model_OffersMapper( );
        $this->mapperSettings = new Admin_Model_SettingsMapper();
	}
    
    public function updateAction(){
        $view = new Zend_View();
        $view->categories = $this->mapperCat->fetchNotEmpty();
        $view->offers = $this->mapperOffers->fetchForPublish();
        $settings = $this->mapperSettings->getSettings();
        $view->name = $settings['yamarketName'];
        $view->company = $settings['yamarketCompany'];
		$view->setBasePath(APPLICATION_PATH.'/modules/yamarket/views');
        $content = $this->_replaceHTMLEntities($view->render('yml/update.phtml'));
        $b = $this->getInvokeArg('bootstrap');
        if (!$b->hasOption('yamarketfile'))
            throw new Zend_Exception("yamarketfile wasn't specified in application.ini");
        $f = fopen('./'.$b->getOption('yamarketfile'),'w');
        fwrite($f,$content);
        fclose($f);
        $this->_helper->json(array('success'=>true));
    }
	
	private function _replaceHTMLEntities($str){
		return str_replace('&nbsp;','&#160;',$str);
	}
}
?>
